package com.sphd.education.adapter;

import android.graphics.Bitmap;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.sphd.education.R;
import com.sphd.education.info.LessonInfo;
import com.sphd.education.interfaces.RecyclerViewOnClickListener;
import com.sphd.education.modle.EducationUrl;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by Administrator on 2015/10/22.
 */
public class SearchLessonRecyclerAdapter extends RecyclerView.Adapter {

    private List<LessonInfo> lessonInfos;
    private int layoutInt;
    private RecyclerViewOnClickListener recyclerViewOnClickListener;
    private DisplayImageOptions displayImageOptions;
    private static final int TYPE_HEADER = Integer.MIN_VALUE;
    private static final int TYPE_FOOTER = Integer.MIN_VALUE + 1;
    private static final int TYPE_ADAPTEE_OFFSET = 2;
    //应来实现上啦加载更多的方法、
    private boolean hasMoreData = true;

    public SearchLessonRecyclerAdapter(List<LessonInfo> lessonInfos, int layoutInt) {
        this.lessonInfos = lessonInfos;
        this.layoutInt = layoutInt;
        displayImageOptions = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.dddddd) //设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.dddddd)//设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.dddddd)  //设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)//设置下载的图片是否缓存在内存中
                .cacheOnDisc(true)//设置下载的图片是否缓存在SD卡中
                .considerExifParams(true)  //是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)//设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型//
                .decodingOptions(new android.graphics.BitmapFactory.Options())//设置图片的解码配置
                        //.delayBeforeLoading(int delayInMillis)//int delayInMillis为你设置的下载前的延迟时间
                        //设置图片加入缓存前，对bitmap进行设置
                        //.preProcessor(BitmapProcessor preProcessor)
                .resetViewBeforeLoading(true)//设置图片在下载前是否重置，复位
                .displayer(new RoundedBitmapDisplayer(20))//是否设置为圆角，弧度为多少
                .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间
                .build();//构建完成
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_view_load_more, parent, false);
            return new FooterViewHolder(view);
        } else {
            final View view = LayoutInflater.from(parent.getContext())
                    .inflate(layoutInt, parent, false);
            if (layoutInt == R.layout.layout_item_lesson) {
                final SearchLessonViewHolder gridViewHold = new SearchLessonViewHolder(view);
                return gridViewHold;
            } else {
                return new SearchLessonViewHolder(view);
            }
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SearchLessonViewHolder) {
            ((SearchLessonViewHolder) holder).position = position;
            ((SearchLessonViewHolder) holder).startTime.setText(" 开学时间:" + lessonInfos.get(position).getBeginTime());
            ((SearchLessonViewHolder) holder).lessonName.setText(lessonInfos.get(position).getName());
            if (lessonInfos.get(position).getLessonDate() != null) {
                ((SearchLessonViewHolder) holder).seeDate.setVisibility(View.VISIBLE);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                String  str = formatter.format(lessonInfos.get(position).getLessonDate());
                ((SearchLessonViewHolder) holder).seeDate.setText("浏览日期："+str);
            }
            ((SearchLessonViewHolder) holder).lessonJianjie.setText(lessonInfos.get(position).getAds());
            ((SearchLessonViewHolder) holder).lessonPrice.setText("￥" + lessonInfos.get(position).getRealTuition());
            ((SearchLessonViewHolder) holder).lessoncity.setText(lessonInfos.get(position).getSchoolName() + "\t" +
                    lessonInfos.get(position).getSchoolArea());
            ImageLoader.getInstance().displayImage(EducationUrl.HttpTitle + "/" + lessonInfos.get(position).getImage(),
                    ((SearchLessonViewHolder) holder).logo, displayImageOptions);
            Log.e("LOGO", lessonInfos.get(position).getName() + "  " + EducationUrl.HttpTitle + "/" + lessonInfos.get(position).getImage());
        } else {
            ((FooterViewHolder) holder).position = position;
            ((FooterViewHolder) holder).setRecyclerViewOnClickListener(recyclerViewOnClickListener);
            //没有更多数据
            if (!hasMoreData) {
                ((FooterViewHolder) holder).mProgressView.stopProgress();
                ((FooterViewHolder) holder).mProgressView.setVisibility(View.GONE);
                ((FooterViewHolder) holder).mTextView.setText("没有更多数据了");
                ((FooterViewHolder) holder).loadingButton.setText("没有了");
                ((FooterViewHolder) holder).loadingButton.setEnabled(false);
            }
        }
    }

    public void setRecyclerViewOnClickListener(RecyclerViewOnClickListener recyclerViewOnClickListener) {
        this.recyclerViewOnClickListener = recyclerViewOnClickListener;

    }

    @Override
    public int getItemCount() {

        return lessonInfos.size() + 1;

    }


    @Override
    public int getItemViewType(int position) {
        if (position == getBasicItemCount()) {
            return TYPE_FOOTER;
        }
        return super.getItemViewType(position);
    }


    public int getBasicItemCount() {
        return lessonInfos.size();
    }

    public boolean hasMoreData() {
        return hasMoreData;
    }

    public void setHasMoreData(boolean isMoreData) {
        if (this.hasMoreData != isMoreData) {
            this.hasMoreData = isMoreData;
            notifyDataSetChanged();
        }
    }


    class SearchLessonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private android.support.v7.widget.CardView lesson_card;
        private int position;
        private TextView startTime;
        private TextView lessonName;
        private TextView lessonJianjie;
        private TextView lessonPrice;
        private TextView lessoncity;
        private TextView seeDate;
        private ImageView logo;

        public SearchLessonViewHolder(View itemView) {
            super(itemView);
            lesson_card = (CardView) itemView.findViewById(R.id.lesson_card);
            startTime = (TextView) itemView.findViewById(R.id.start_time);
            seeDate = (TextView) itemView.findViewById(R.id.see_date);
            lessonJianjie = (TextView) itemView.findViewById(R.id.lesson_jianjie);
            lessonName = (TextView) itemView.findViewById(R.id.lesson_name);
            lessonPrice = (TextView) itemView.findViewById(R.id.lesson_price);
            lessoncity = (TextView) itemView.findViewById(R.id.school_city);
            logo = (ImageView) itemView.findViewById(R.id.ic_launcher_img);
            lesson_card.setOnClickListener(this);
        }


        @Override
        public void onClick(View v) {
            recyclerViewOnClickListener.onTouch(v, position);
        }
    }

}
